import {VxeGridProps, VxeGridPropTypes} from "vxe-table/types/grid";
import { VxeTableDefines } from "vxe-table/types/table";
// 定义扩展的列类型，添加了 search 属性
interface CustomColumn extends VxeTableDefines.ColumnOptions {
  search?: boolean; // 可选的 search 属性
  searchType?: 'VxeInput';
  searchSlot?: boolean;
  searchValue?: any;
  span?: number;
  hideColumn?: boolean;
}

interface TableSetting {
  columns: CustomColumn[]; // 自定义列类型数组
}

const columns: TableSetting['columns'] = [
  // { field: 'depId', width: 70, searchValue: '', hideColumn: true },
  { type: 'checkbox', width: 60 },
  { type: 'seq', width: 60, title: '#' },
  { field: 'deptName', title: '所属租户',  search: true, searchType: 'VxeInput' },
  { field: 'shortName', title: '角色名称',  search: true, searchType: 'VxeInput' },
  { field: 'deptCode', title: '角色别名',  search: true, searchType: 'VxeInput'  },
  { field: 'deptLevel', title: '角色排序',  },
  { field: 'menu', title: '操作', width: 250, fixed: 'right',slots: { default: 'menu' }  },
]

export function getSettings(): VxeGridPropTypes {
  const formConfig = {
    // 将 columns 中 search 为 true 的字段的 field 属性提取出来，并生成对象
    data: columns
      .filter(v => v.search) // 筛选出 search 为 true 的列
      .reduce((acc, column) => {
        if (column.field) {
          acc[column.field] = column.searchValue || null; // 使用 field 作为键，值初始化为 null
        }
        return acc;
      }, {} as Record<string, any>), // 初始值为空对象

    items: [...columns
      .filter(v => v.search)
      .map(v => {
        const item = {
          field: v.field,
          title: v.title,
          span: v.span || 6,
          itemRender: { name: v.searchType },
        }
        if(v.searchSlot) {
          item['slots'] = { default: `${v.field}Search` }
        }

        return item
      }), { span: 24, align: 'right', itemRender: {}, slots: { default: 'actionItem' } }]
  };
  return {
    size: 'mini',
    showOverflow: true,
    border: true,
    loading: false,
    height: 500,
    align: 'center',
    pagerConfig: {
      total: 0,
      currentPage: 1,
      pageSize: 10,
      layouts: [
        'Sizes',
        'PrevJump',
        'PrevPage',
        'Number',
        'NextPage',
        'NextJump',
        'FullJump',
        'Total'
      ]
    },
    formConfig,
    columns: columns.filter(v => !v.hideColumn), // 使用传入的列配置
    data: []        // 使用传入的数据
  }
}
